import 'package:dio/dio.dart';
import 'package:flutter_base/http/cookie_interceptor.dart';
import 'package:flutter_base/http/http_method.dart';
import 'package:flutter_base/http/print_log_interceptor.dart';
import 'package:flutter_base/http/rsp_interceptor.dart';
import 'package:flutter_base/page/common/loading_toast.dart';
import 'package:oktoast/oktoast.dart';

///
/// @DIR_PATH：lib/http
/// @TIME：2024/6/4 20:28
/// @AUTHOR：starr
///
class DioInstance {
  static DioInstance? _instance; //静态私有变量
  DioInstance._(); //静态的私有构造方法

  //实例为空则初始化
  static DioInstance getInstance() {
    return _instance ??= DioInstance._();
  }

  //初始化dio对象
  final Dio _dio = Dio();

  //
  final _defaultTime = const Duration(seconds: 30);

  void initDio({
    required String baseUrl, //required必填参数
    String? httpMethod = HttpMethod.GET,
    Duration? connectTimeout,
    Duration? receiveTimeout,
    Duration? sendTimeout,
    ResponseType? responseType = ResponseType.json,
    String? contentType,
  }) {
    _dio.options = BaseOptions(
      baseUrl: baseUrl,
      method: httpMethod,
      connectTimeout: connectTimeout ?? _defaultTime,
      receiveTimeout: receiveTimeout ?? _defaultTime,
      sendTimeout: sendTimeout ?? _defaultTime,
      responseType: responseType,
      contentType: contentType,
    );

    //当App运行在Release环境时，inProduction为true；
    // 当App运行在Debug和Profile环境时，inProduction为false。
    bool inProduction = const bool.fromEnvironment("dart.vm.product");

    _dio.interceptors.add(CookieInterceptor());
    if (!inProduction) {
      debugFunction();
    }
    _dio.interceptors.add(ResponseInterceptor());
  }


  get({
    required String path,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async{
    Response? response;
    try {
      Loading.show();
      response = await _dio.get(path,
          queryParameters: params,
          options: options ?? Options(
              method: HttpMethod.GET,
              receiveTimeout: _defaultTime,
              sendTimeout: _defaultTime
          ),
              cancelToken: cancelToken);
      Loading.dismissAll();
    }on DioException catch(e){
      showToast(e.toString());
      Loading.dismissAll();
    }
    return response?.data;

  }

  post({
    required String path,
    Object? data,
    Map<String, dynamic>? params,
    Options? options,
    CancelToken? cancelToken,
  }) async{
    Response? response;
    try {
      response = await _dio.post(path,
          data: data,
          queryParameters: params,
          options: options ?? Options(
              method: HttpMethod.POST,
              receiveTimeout: _defaultTime,
              sendTimeout: _defaultTime
          ),
          cancelToken: cancelToken);
    }on DioException catch(e){
      showToast(e.toString());
    }
    return response?.data;
  }

  void debugFunction() {
    //打印输出拦截器
    _dio.interceptors.add(PrintLogInterceptor());
  }


  DioInstance changeBaseUrl(String baseUrl){
    _dio.options.baseUrl = baseUrl;
    return this;
  }
}